package com.sky.controller.admin;


import com.atom.module.common.result.PageResult;
import com.atom.module.common.result.Result;
import com.sky.pojo.dto.SetMealDTO;
import com.sky.pojo.dto.SetMealPageQueryDTO;
import com.sky.pojo.vo.SetmealVO;
import com.sky.service.SetMealService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "套餐相关Admin接口")
@RequestMapping("/admin/setmeal")
@RestController("adminSetmealController")
@Slf4j
public class SetMealController {


    @Autowired
    private SetMealService setmealService;

    //  清理指定分类下面的套餐缓存
    @CacheEvict(cacheNames = "setmeal", key = "#setmealDTO.categoryId")
    @Operation(summary = "新增套餐")
    @PostMapping
    public Result<?> save(@RequestBody SetMealDTO setmealDTO) {
        log.info("新增套餐：{}", setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Result<PageResult> page(SetMealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    // @CacheEvict: 清除setmeal下全部缓存数据
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @Operation(summary = "删除套餐")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除套餐：ids = {}", ids);
        setmealService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询套餐，用于修改页面回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("根据id查询套餐: id={}", id);
        SetmealVO setmealVO = setmealService.getByIdWithDish(id);
        return Result.success(setmealVO);
    }


    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    // @CacheEvict: 清除setmeal下全部缓存数据
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @PutMapping
    @Operation(summary = "修改套餐")
    public Result update(@RequestBody SetMealDTO setmealDTO) {
        log.info("修改套餐: setmealDTO={}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售停售
     *
     * @param status
     * @param id
     * @return
     */
    // @CacheEvict: 清除setmeal下全部缓存数据
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @PostMapping("/status/{status}")
    @Operation(summary = "套餐起售停售")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("套餐起售停售: status={},id={}", status, id);
        setmealService.startOrStop(status, id);
        return Result.success();
    }

}
